/* 
描述：定义用户管理模块的相关后端接口 
完成人：何艺珲
完成时间: 2025/04/26
*/
package com.jiesenhe.qacommunity.controller;

import com.jiesenhe.qacommunity.dto.GetEasyUserInfoResponseDTO;
import com.jiesenhe.qacommunity.dto.GetUserInfoResponseDTO;
import com.jiesenhe.qacommunity.dto.LoginResponseDTO;
import com.jiesenhe.qacommunity.pojo.User;
import com.jiesenhe.qacommunity.service.UserService;
import com.jiesenhe.qacommunity.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @GetMapping("/login")
    public ResponseEntity<HashMap<String, Object>> login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        int type = user.getType();
        User user1 = userService.getUsernameAndPassword(username, password);
        if (user1 != null&& user1.getType() == type){
            user1.setToken(JwtUtil.createToken(username,password,type));
            LoginResponseDTO responseDTO = new LoginResponseDTO(user1);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 1);
                    put("msg",  "login success");
                    put("data", responseDTO);
                }
            });
        }else{
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "login failed");
                    put("data", null);
                }
            });
        }

    }
    @GetMapping("/getUserInfo")
    public ResponseEntity<Object> getUserInfo(HttpServletRequest request,User user) {
        String token = request.getHeader("Token");
        if (JwtUtil.checkToken(token)){
            int id = user.getId();
            User user1 = userService.getUserInfo(id);
            GetUserInfoResponseDTO getUserInfoResponseDTO = new GetUserInfoResponseDTO(user1);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", getUserInfoResponseDTO != null ? 1 : 0);
                    put("msg",  getUserInfoResponseDTO != null  ? "getUserInfo success" : "getUserInfo failed");
                    put("data", getUserInfoResponseDTO);
                }
            });
        }else{
            int id = user.getId();
            User user1 = userService.getUserInfo(id);
            GetEasyUserInfoResponseDTO getEasyUserInfoResponseDTO = new GetEasyUserInfoResponseDTO(user1);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", getEasyUserInfoResponseDTO != null ? 1 : 0);
                    put("msg",  getEasyUserInfoResponseDTO != null  ? "getEasyUserInfo success" : "getEasyUserInfo failed");
                    put("data", getEasyUserInfoResponseDTO);
                }
            });
//            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
//                {
//                    put("code", 0);
//                    put("msg", "token check failed");
//                    put("data", null);
//                }
//            });
        }
    }


    @PostMapping("/register")
    public ResponseEntity<Object> register(@RequestBody User user) {
        List<User> checkList = userService.registerCheck(user);
        if (checkList != null && !checkList.isEmpty()){
            System.out.println("________________________________检查不通过，数据库有重复数据_______________________________");
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code",0);
                    put("msg","register failed");
                    put("data", null);
                }
            });
        }else {
            System.out.println("________________________________检查完成，数据库无重复数据_______________________________");
//            return null;
            int insertCode = userService.registerInsert(user);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", insertCode != 0 ? 1 : 0);
                    put("msg", "register success");
                    put("data", null);
                }
            });
        }

    }

    @GetMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("Token");
        return JwtUtil.checkToken(token);
    }

    @PostMapping("/updateUserInfo")
    public ResponseEntity<Object> updataUserInfo(@RequestBody User user, HttpServletRequest request){

        String token = request.getHeader("Token");
        if (JwtUtil.checkToken(token)){
            int updataCode = userService.updateUserInfo(user);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", updataCode != 0 ? 1 : 0);
                    put("msg", "success");
                    put("data", null);
                }
            });
        }else {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed");
                    put("data", null);
                }
            });
        }


    }

}
